/**
 * (C) 2007 - Boston University
 * 	Contact: Michael Ocean 

  This work is a part of the larger snBench project.  Information
  can be found at: http://csr.bu.edu/snbench

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
package snbenchlib;

import java.util.Collection;
import java.util.HashMap;
import java.util.Random;

import step.DuplicateID;

/**
 * @author Michael
 *
 *
 * A IDManagedHashTable maintains a hashmap of entries where keys are
 * provided by the caller.  The map is not static so multiple IDManagedHTs
 * can exist.
 *  
 * E.g., a UIDGenerator instance could ensure that each STEP node is
 * unique and another instance for, say, Image Sensors
 * 
 * Also stores the elements as a HashMap
 * 
 * 
 * IDManagedHashTable
 * snbenchlib / IDManagedHashTable.java
 */
public class IDManagedHashTable <T>{
    // Combination Unique ID generator and hash storage 
	
    private HashMap<String, T> idmap;
    
    public IDManagedHashTable() {
	    idmap = new HashMap<String, T>();
    }
        
    public UID RequestNextID(String Prefix) {
		String newkey;
		String GeneratedSuffix = "";
		int pad = 0;	
		Random r = new Random();
		newkey = Prefix;
		while (idmap.containsKey(newkey)) {
			pad +=10;
			GeneratedSuffix = String.valueOf(r.nextInt(pad));
			newkey = Prefix + "_" + GeneratedSuffix;
		}
		return new UID(newkey);
    }

    public UID RegisterID(String Prefix, T data)  {
		UID id =  RequestNextID(Prefix);
    	String sk = id.get();
		idmap.put(sk, data);
		return id;
	    }
    
    public void RegisterID(UID id, T data) throws DuplicateID {
		String sk = id.get();
		if (idmap.containsKey(sk))
		    throw new DuplicateID(sk);
		idmap.put(sk, data);
	    }
    
    /**
     * Tries to remove the entity stored under key 'id'
     * returns true if something done
     * 
     * @param id - the key of the entity to be removed
     * @return - true if something done
     */
	public boolean RemoveID(UID id){
    	if(idmap==null)
    		return false;
		if (idmap.containsKey(id.get())){
		    idmap.remove(id.get());
		    return true;
		}
		return false;
   }

	public void RemoveAllIDs(){
	    idmap = new HashMap<String, T>();
	}
	
	/**
	 * returns the value stored at key 'id'
	 * (or null if no item is found)
	 * 
	 * @param id
	 * @return
	 */
	public T Get(UID id){
		return idmap.get(id.get());
	}

	/**
	 * returns and removes the value stored at key 'id'
	 * (or null if no item is found)
	 * 
	 * @param id
	 * @return
	 */
	public T Remove(UID id){
		return idmap.remove(id.get());
	}
	
	public Collection<T> Values(){
		return idmap.values();
	}
 	
	public int Size(){
		return idmap.size();
	}
}
